grobotsfandomcom-20200213-history
Decision making
This article is about ways that can be used to represent a decision making in a program. It is not important for simple sides but when your sides code will grow large enough you will more and more often find yourself solving a priority problem. Should we run from an enemy or eat the food? Should we chase for the enemy or guard our civilians? If-else branching Simplest and the most common way to implement decision making is to simply put all the requirement like the distance, energy and robots found together to make a decision right away. It works as long as you dont have more than 4 tasks, or behaivors or states, but then it becomes very hard to follow the code or change it. But for the simplicity this method deserve to be the first to think about. Especially when calculations are so expensive. main problem with this method is to avoid several cases being activated at once. For example in one part of the code we are checking should we run for food or not, and in another we are checking should we dodge or not. If both cases will be activated then cell most likely will fail at both tasks. To avoid this author has to either use a deep if-else tree or save each activity for later cheking. For example we save our dodging trajectory and the fact that we need to todge. And when we will check the food we will also check thst we are not dodging. Eventually when the code will become more complex this method will develop into a next one, state machine. State machine Following this way author implements all his states separately, for exaple eating, dodging, wandering, resting, attacking, running away, etc. And then each state has either links in another states (using labels) or change the current state variable (using loops). Idea is that each state is not calculated each time. And each state does not interconnected with all other states. Only with few related to it. For example: eating state may be connected with dodging state and wandering state only. As if we dont stop eat untill somebody would attack us or food will run out. Then dodging state connected with eating and wandering and attacking states. We cant go to resting state right after the dodging state. Such ways make it easier to follow the code because amount of ways how behavior can change is smaller and easier to keep in mind all together. This way is useful when you have up to 10 behaivors. Main problem with this way is that you cant have several states being active at once. For example it may be useful to dodge and eat at the same time, but with state machine it would require to implement such state as dodgin-and-eating, instead of activating both of them. Another way to have several active state is by making them paralel, without any connections between them. For instance your cell may have a movement and weaponry being separated and controlled independently. Then your movement system may be in "eating" state and your weaponry system may be in "attacking" state. Heatmap / gravitational model This is one of the most complex way to code and one of the easiest to debug or understand. Unfortunately it is also very complex in term of CPU used, at least at the grobot scale. To implement such way an author need to give the importance weight and formula for each object around the cell. For example for the food it may be: energy/distance^2. for enemy robots formula may be -1*size/distance^2. When you want to keep some objects close, but not too close you may use exponent formula. for example to softly keep the missiles at the distance of 15 you may use this formula exp(-((distance-15)/10)^2). Another question is how to calculate this field, knowing all the "sources" of a singnal on our field. Simplest way is to calculate few points around your cell, for example a -0.1;-0.1 0.1;-0.1 -0.1;0.1 0.1;0.1. by comparing how good each point is and choosing the average you may obtain a direction that may have any angle, not just n*(pi/2). This way is easy to understand but hard to calculate. Another way is to calculate gradient right under your cell. Because it is very hard to build a 2d formula from all your sources you can calculate gradient from each of your sources in 1D and then sum them together. For example: when calculating the food we obtained a gradient that shows that we should move forvard with strength of this decision of 0.5. but when we calculated the evasion we got that our cell should move backward with the strength of this decision of 1. then simply sum this vectors, and cell will move backward avoiding shot and probably moving closer to another food in a way. This type of decision making dont need states or if-else combinations, as long as you can make a good formula for your objects around you. Sometimes this can be very hard. P.S. one of the hardest part with this model is to stop the cell right above the food. hint is to use engine at full power to achieve zero velocity. Pheromones Inspired by segregated eater I've made a side that was writing footprints of each ally in one "map" and presence of enemies in another "map". It did work somewhat well when it omes to collecting food. cells was going in a patters that ensures that all map cells are checked with equal time intervals. But in practice it is easier to make a cell that sits still and collect the food using a huge food sensor and force field. When it comes to a fight it did not work at all, even when I added a spreading of enemy presence through the map. fighters taked too much time to sense an enemy if they were not close enough. Anyway, ants are really cool if they can survive using such primitive algorythm, but personally I think for a grobot pheromones are working too slowly and takes too much computations and even mode coding time. Neural network and genetic algorythm Reserved for future. Right now there is no way to extract the data after the match to save the progress. In theory this algorithmwill behave closely to gravitational model but will do it by itself, with no need for complex mathematics being involved more than once, at the creation of this algorithm. But to create this in a first place even more complex math should be involved.